<title><i class="fa fa-users"></i> 用户管理</title>

<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <a><cite>系统管理</cite></a>
        <a><cite>用户管理</cite></a>
    </div>
</div>

<div class="layui-fluid layui-anim layui-anim-upbit" id="NP_user_manage">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="layadmin-userfront-formlist">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">登录账号</label>
                    <div class="layui-input-block">
                        <input type="text" name="username" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">用户昵称</label>
                    <div class="layui-input-block">
                        <input type="text" name="nickname" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">电子邮箱</label>
                    <div class="layui-input-block">
                        <input type="text" name="email" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline" style="width: 180px;">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-block">
                        <select name="status">
                            <option value="">全部</option>
                            <option value="false">禁用</option>
                            <option value="true">正常</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-user-front-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-useradmin" data-type="disable"><i class="fa fa-lock"></i> 锁定/解锁</button>
                <button class="layui-btn layuiadmin-btn-useradmin" data-type="delete" style="background-color: #FF5722;"><i class="fa fa-minus-square-o"></i> 彻底删除</button>
            </div>

            <table id="LAY-user-manage" lay-filter="LAY-user-manage"></table>

            <script type="text/html" id="table-useradmin-webuser">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-bg-cyan layui-btn-xs" lay-event="recharge"><i class="layui-icon layui-icon-dollar"></i>充值</a>
            </script>
        </div>
    </div>
</div>

<script>
    layui.use(['table', 'form', 'admin'], function () {
        var $ = layui.$
            , admin = layui.admin
            , setter = layui.setter
            , view = layui.view
            , table = layui.table
            , form = layui.form;

        //用户管理
        var userTable = table.render({
            elem: '#LAY-user-manage'
            , url: NP_Config.domain + '/admin/user'
            , size: 'lg'
            , cols: [[
                {type: 'radio', fixed: 'left'}
                , {field: 'id', width: 80, title: 'ID', sort: true}
                , {
                    field: 'nickname', title: '用户昵称', minWidth: 150, templet: function (d) {
                        return '<img src="' + (d.avatar.startsWith("http") ? '' : NP_Config.domain) + d.avatar + '" style="width: 40px;height: 40px;border: 1px solid #c2c2c2;" alt="' + d.username + '" class="layui-circle"/>&nbsp;&nbsp;' + d.nickname;
                    }
                }
                , {
                    field: 'username', title: '登录账号', sort: true, minWidth: 150, templet: function (d) {
                        return '<b>' + d.username + '</b>';
                    }
                }
                , {
                    title: '硬币', width: 120, templet: function (d) {
                        return '<a class="showCoin" data-id="' + d.id + '" style="cursor:pointer;color: #00a2d4;">查看硬币</a>';
                    }
                }
                , {field: 'gmtCreate', title: '注册时间', sort: true, minWidth: 180}
                , {field: 'email', title: '电子邮箱', width: 180}
                , {field: 'lastLoginIp', title: '最后登录IP', width: 150}
                , {field: 'lastLoginAddr', title: '最后登录地址', width: 150}
                , {
                    field: 'status', title: '状态', minWidth: 80, templet: function (d) {
                        return d.status ? '<span class="layui-badge layui-bg-blue">正常</span>' : '<span class="layui-badge">禁用</span>';
                    }
                }
                , {title: '工具栏', minWidth: 180, align: 'center', toolbar: '#table-useradmin-webuser', fixed: 'right'}
            ]]
            , where: {
                extra: {
                    nickname: $("#NP_user_manage input[name=nickname]").val(),
                    username: $("#NP_user_manage input[name=username]").val(),
                    email: $("#NP_user_manage input[name=email]").val(),
                    status: $("#NP_user_manage select[name=status]").val()
                }
            }
        });

//排序
        table.on('sort(LAY-user-manage)', function (obj) {
            userTable.reload({
                initSort: obj
                , where: {
                    sort: obj.field
                    , order: obj.type
                }
            });
        });

        //监听工具条
        table.on('tool(LAY-user-manage)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                admin.popup({
                    title: '编辑用户'
                    , offset: '150px'
                    , area: ['500px', '550px']
                    , id: 'LAY-popup-user-add'
                    , success: function (layero, index) {
                        view(this.id).render('system/dialog/userform', data).done(function () {
                            form.render(null, 'layuiadmin-form-useradmin');

                            //监听提交
                            form.on('submit(LAY-user-front-submit)', function (data) {
                                var field = data.field; //获取提交的字段

                                admin.req({
                                    url: NP_Config.domain + "/admin/user/update"
                                    , type: "post"
                                    , data: field
                                    , done: function (resp) {
                                        layer.msg(resp.message);
                                        //提交 Ajax 成功后，关闭当前弹层并重载表格
                                        layui.table.reload('LAY-user-manage'); //重载表格
                                    }
                                });
                                layer.close(index); //执行关闭
                            });
                        });
                    }
                });
            } else if (obj.event === "recharge") {
                layer.prompt({
                    formType: 0,
                    value: '0',
                    title: '请输入充值的硬币数量'
                }, function (value, index, elem) {
                    admin.req({
                        url: NP_Config.domain + "/admin/user/recharge"
                        , type: "post"
                        , data: {userId: data.id, coin: value}
                        , done: function (resp) {
                            layer.close(index);
                            layer.msg(resp.message);
                            userTable.reload();
                        }
                    });
                });
            }
        });


        form.render(null, 'layadmin-userfront-formlist');

        //监听搜索
        form.on('submit(LAY-user-front-search)', function (data) {
            table.reload('LAY-user-manage', {
                where: {
                    extra: {
                        nickname: data.field.nickname,
                        username: data.field.username,
                        email: data.field.email,
                        status: data.field.status
                    }
                }
            });
        });

        //事件
        var active = {
            disable: function () {
                var checkStatus = table.checkStatus('LAY-user-manage')
                    , checkData = checkStatus.data; //得到选中的数据

                if (checkData.length !== 1) {
                    return layer.msg('请选中一个用户');
                }

                layer.confirm('确定【锁定／解锁】吗？', function () {

                    admin.req({
                        url: NP_Config.domain + '/admin/user/disable'
                        , type: "post"
                        , data: {
                            id: checkData[0].id
                            , status: !checkData[0].status
                        }
                        , done: function (resp) {
                            if (resp.code === NotePress.Constant.CODE_SUCCESS) {
                                table.reload('LAY-user-manage');
                            }
                            layer.msg(resp.message);
                        }
                    });
                });


            }
            , delete: function () {

                var checkStatus = table.checkStatus('LAY-user-manage')
                    , checkData = checkStatus.data; //得到选中的数据

                if (checkData.length !== 1) {
                    return layer.msg('请选中一个用户');
                }

                layer.prompt({
                    formType: 1
                    , title: '敏感操作，请验证口令'
                }, function (value, index) {
                    admin.req({
                        url: NP_Config.domain + "/admin/session/checkPass"
                        , type: "post"
                        , data: {
                            md5Pass: md5(value)
                        }
                        , done: function () {
                            //验证通过
                            layer.close(index);
                            layer.confirm('确定删除吗？', function (i) {

                                admin.req({
                                    url: NP_Config.domain + '/admin/user/delete'
                                    , type: "post"
                                    , data: {id: checkData[0].id}
                                    , done: function () {
                                        layer.close(i);
                                        table.reload('LAY-user-manage');
                                    }
                                });
                            });

                        }
                    })
                });
            }
        };

        $('.layui-btn.layuiadmin-btn-useradmin').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        $("body").on("click", "a.showCoin", function () {
            var $this = $(this);
            var userId = $this.attr("data-id");
            admin.req({
                url: NP_Config.domain + "/admin/user/getCoin",
                data: {
                    userId: userId
                }, done: function (resp) {
                    $this.text(resp.data);
                }
            })
        });


    });
</script>